package net.orange.architecture.process;

import java.util.ArrayList;
import java.util.Collection;

import net.orange.architecture.db.Conditions;
import net.orange.architecture.db.IObjectManager;
import net.orange.architecture.db.ListConditions;
import net.orange.architecture.model.BaseValueObject;
import net.orange.architecture.system.SystemSequence;

/**
 * The base business logical process object which implement the IBaseProcess
 * interface.Every business logical object should be extended from the base
 * value object.
 */
public abstract class BaseProcess implements IBaseProcess {

	protected IObjectManager dao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * net.orange.architecture.process.IBaseProcess#setDao(net.orange.architecture
	 * .db.IBaseDao)
	 */
	public void setDao(IObjectManager dao) {
		this.dao = dao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.process.IBaseProcess#GetDao()
	 */
	public IObjectManager getDao() throws Exception {
		return dao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * net.orange.architecture.process.IBaseProcess#list(net.orange.architecture
	 * .db.ListConditions)
	 */
	public Collection list(ListConditions cds) throws Exception {

		return dao.list2(cds);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.process.IBaseProcess#listAll()
	 */
	public Collection listAll() throws Exception {
		return listAll(new Conditions());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * net.orange.architecture.process.IBaseProcess#listAll(net.orange.architecture
	 * .db.Conditions)
	 */
	public Collection listAll(Conditions cds) throws Exception {
		return dao.list(cds);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.process.IBaseProcess#find(long)
	 */
	public BaseValueObject find(long id) throws Exception {
		return (id > 0) ? dao.find(id) : null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.process.IBaseProcess#find(java.lang.String)
	 */
	public BaseValueObject find(String field, String code) throws Exception {
		return (code != null && code.trim().length() > 0) ? (BaseValueObject) dao
				.find(field, code) : null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * net.orange.architecture.process.IBaseProcess#insert(net.orange.architecture
	 * .vo.BaseValueObject)
	 */
	public boolean insert(BaseValueObject bvo) throws Exception {
		bvo.setPk(SystemSequence.getSequence());
		return dao.create(bvo);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * net.orange.architecture.process.IBaseProcess#update(net.orange.architecture
	 * .vo.BaseValueObject)
	 */
	public boolean update(BaseValueObject bvo) throws Exception {
		return dao.update(bvo);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.process.IBaseProcess#delete(long)
	 */
	public boolean delete(BaseValueObject bvo) throws Exception {
		return dao.delete(bvo);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * net.orange.architecture.process.IBaseProcess#checkMe(net.orange.architecture
	 * .vo.BaseValueObject, java.lang.String[])
	 */
	public boolean checkSave(BaseValueObject bvo, boolean n,
			Collection<String> msg) throws Exception {
		if (msg == null)
			msg = new ArrayList<String>();

		return bvo.satisfy(msg);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.process.IBaseProcess#checkDelete(net.orange.
	 * architecture.model.BaseValueObject, java.util.Collection)
	 */
	public boolean checkDelete(BaseValueObject bvo, Collection<String> msg)
			throws Exception {
		if (msg == null)
			msg = new ArrayList<String>();

		return bvo.canRemove(msg);
	}
}
